package com.ccl.iot.device;

import com.ccl.iot.IOTDataType;
import com.ccl.iot.IOTNetAPIEnum.EIOT_NETWORK_TYPE;
import com.ccl.iot.IOTObject;
import com.ccl.iot.R;
import com.ccl.iot.object.IOTDeviceInfoEx;
import com.ccl.iot.privates.DimmerTurnSpeedDialog;
import com.ccl.iot.privates.QueueInvokeBuffer;
import com.ccl.iot.privates.QueueInvokeBuffer.InvokeDelegate;

public class IOTDimmer extends IOTSwitch implements DimmerTurnSpeedDialog.DimmerTurnSpeed, IOTObject.IOTDVRInner {

	private long mRegState = 0;

	private static final int TURN_SPEED_ADDRESS = 0;
	private static final int DIMMER_ADDRESS = 6;

	private short mTurnSpeed = 0;		//开关延时	单位: 秒
	private short mDimmer = 0;			//当前亮度
	private short mTurnDimmer = 0;		//快速开关亮度

	private boolean beforeState = false;
	
	private long mLastTime = 0;

	private QueueInvokeBuffer<byte[]> mReomteBuffer;

	private boolean mClickTurn;
	
	private boolean mDimming;

	public IOTDimmer(long aDevID, IOTDeviceInfoEx aDeviceInfo, int aSwitchAddress, int aSwitchSize) {
		super(aDevID, aDeviceInfo, aSwitchAddress, aSwitchSize);
	}

	@Override
	protected void setStateAddress(int aStateAddress) {
		iStateAddress = aStateAddress;

		mDefIndex = (iStateAddress - IOTDeviceConst.TDEVREG_ADDRESS.EDEVREG_DIMMER_ADDRESS_STATE_1);

		if (mDefIndex > 0) {
			mDefIndex /= IOTDeviceConst.TDEVREG_SIZE.EDEVREG_DIMMER_SIZE_STATE;
		}
		mDefIndex++;
	}

	public void setTurnSpeed(byte[] speed) {
		int address = getRegisterAddress() + TURN_SPEED_ADDRESS;
		iRegisterInterface.writeRegister(this, address, speed.length, speed, getNetWorkState());
	}

	public void setDimmer(int state, boolean isStop) {
		if(state <= 10){
			state = 0;
		}
		
		byte[] data = new byte[2];
		data[0] = (byte) (state & 0xff);
		data[1] = (byte) (state >> 8 & 0xff);
//		final long id = getID();
		final int address = getRegisterAddress() + DIMMER_ADDRESS;
		final int netWorkState = getNetWorkState();
		if (netWorkState == EIOT_NETWORK_TYPE.EIOT_NETWORK_TYPE_LOCAL) {
			if(isStop){
				iRegisterInterface.writeRegister(this, address, data.length, data, EIOT_NETWORK_TYPE.EIOT_NETWORK_TYPE_BOTH);
			}else{
				iRegisterInterface.writeRegister(this, address, data.length, data, netWorkState);
			}
			
		} else {
			if (mReomteBuffer == null) {
				mReomteBuffer = new QueueInvokeBuffer<byte[]>("remote dimmer buffer", 200, new InvokeDelegate<byte[]>() {
					int tmp = -1;

					@Override
					public void onInvoke(byte[] aObject) {
						int state = ((int) aObject[1] << 8) | (((int) aObject[0]) & 0xff);
						if (state == 0 || tmp / 10 != state / 10) {
							tmp = state;
							iRegisterInterface.writeRegister(IOTDimmer.this, address, aObject.length, aObject, netWorkState);
						}
					}

					@Override
					public void lastTask(final byte[] aObject) {
						mDimming = false;
						int state = ((int) aObject[1] << 8) | (((int) aObject[0]) & 0xff);
						if (mDimmer != state) {
							mReomteBuffer.put(aObject);
						}
					}

				});
			}

			mReomteBuffer.put(data);
		}
	}
	
//	public void onLastDimmer(){
//		iRegisterInterface.writeRegister(id, address, data.length, data, netWorkState);
//	}

	@Override
	public boolean isOn() {
		if (mDimmer == 0) {
			iState = 0;
			return false;
		} else {
			iState = 1;
			return true;
		}
	}

	@Override
	public void turn(boolean aON) {
		mClickTurn = true;
		if (aON) { // open
			iState = 1;
			setDimmer(mTurnDimmer, true);
		} else { // close
			iState = 0;
			setDimmer(0, true);
		}
	}

	@Override
	public void updateRegister(int aAddress, int aSize, byte[] aData, int aDataPos, int aNetWork, long time) {
		if(mLastTime > time && time != 0){
			return;
		}

		saveRegister(aAddress, aSize, aData, aNetWork);
		mLastTime = time;
		
		if(aData.length >= 2){
			if(aAddress + aSize == getRegisterAddress()+getRegisterSize()){
				if (aAddress == getRegisterAddress() && aSize == getRegisterSize()){
					mTurnSpeed = IOTDataType.C16(aData, aDataPos);
					
					mTurnDimmer = IOTDataType.C16(aData, aDataPos+DIMMER_ADDRESS-2);
					
				}
				if(aData.length <= getRegisterSize()/2){
					mDimmer = IOTDataType.C16(aData, aData.length-2);
					
					if(aData.length - 4 >= 0){
						mTurnDimmer = IOTDataType.C16(aData, aData.length-4);
					}
				}else{
					mDimmer = IOTDataType.C16(aData, aDataPos+getRegisterSize()-2);
					
					if(aAddress != getRegisterAddress() || aSize != getRegisterSize()){
						mTurnDimmer = IOTDataType.C16(aData, aData.length-4);
					}
				}
				
			}else if(aAddress + aSize < getRegisterAddress()+getRegisterSize()){
				mTurnSpeed = IOTDataType.C16(aData, aDataPos);
			}
		}else if(aData.length == 1){
			
			if(aAddress == getRegisterAddress()+getRegisterSize() -1 ){
				byte[] c8Array = IOTDataType.C8Array(mDimmer);
				c8Array[1] = aData[0];
				mDimmer = IOTDataType.C16(c8Array, 0);
			}else if(aAddress == getRegisterAddress()+getRegisterSize() - 2 ){
				byte[] c8Array = IOTDataType.C8Array(mDimmer);
				c8Array[0] = aData[0];
				mDimmer = IOTDataType.C16(c8Array, 0);
			}else if(aAddress == getRegisterAddress() ){
				byte[] c8Array = IOTDataType.C8Array(mTurnSpeed);
				c8Array[0] = aData[0];
				mTurnSpeed = IOTDataType.C16(c8Array, 0);
			}else if(aAddress == getRegisterAddress()+1 ){
				byte[] c8Array = IOTDataType.C8Array(mTurnSpeed);
				c8Array[1] = aData[0];
				mTurnSpeed = IOTDataType.C16(c8Array, 0);
			}
		}
		if(beforeState != isOn()){
			afterStateChange();
			if(mClickTurn && (mDimmer == mTurnDimmer || mDimmer == 0) && mListener != null){
				mListener.onTurn();
				mClickTurn = false;
			}
			beforeState = isOn();
		}
		
		if(mDimming == false && mListener != null){
			mListener.onTurn();
		}
	}

	public short getDimmer() {
		return mDimmer;
	}
	
	public short getTurnDimmer(){
		return mTurnDimmer;
	}

	@Override
	public String getDeviceName() {
		return getName();
	}

	public short getTurnSpeed() {
		return mTurnSpeed;
	}

	@Override
	public byte[] getRegister() {
		mRegState = (((long) mDimmer) << 48) | mTurnSpeed;
		return IOTDataType.C8Array(mRegState);
	}
	
	public void onStartDimmer(){
		mDimming = true;
	}

	public void onStopDimmer(final int progress) {
		
	}

	@Override
	public int getCornerId() {
		return R.drawable.icon_brightness;
	}

	public interface OnDimmerTurnListner {
		public void onTurn();
	}

	private OnDimmerTurnListner mListener;

	public void setOnDimmerTurnListener(OnDimmerTurnListner listener) {
		mListener = listener;
	}
	
}
